package com.dodo.blog.ui.component.composite;

import com.dodo.blog.WebApplication;
import com.dodo.blog.ui.component.container.Panel;
import com.dodo.blog.ui.component.input.Anchor;
import com.dodo.blog.ui.component.simple.Span;
import com.dodo.blog.ui.page.Page;
import com.dodo.blog.ui.page.publish.About;
import com.dodo.blog.ui.util.FormatUtil;
import com.dodo.blog.ui.util.UriConstructor;

import java.util.Calendar;

import static com.dodo.blog.ui.util.Localization.localize;

/**
 * @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
 */
public class FooterContentPanel
        extends Panel
{

    public FooterContentPanel( Class<? extends Page>... pages )
    {
        setClassName( "footer-content" );

        // menu panel
        Panel menuWrapper = new Panel();
        add( menuWrapper );
        int s = 0;
        for ( Class<? extends Page> page : pages )
        {
            if ( s > 0 )
            {
                menuWrapper.add( getSeparator() );
            }
            menuWrapper.add( new Anchor( localize( "menu." + page.getSimpleName().toLowerCase() ), page ) );
            s++;
        }

        // archive panel
        Panel archiveWrapper = new Panel();
        add( archiveWrapper );
        Calendar calendar = Calendar.getInstance();
        s = 0;
        for ( int i = 0; i < 5; i++ )
        {
            if ( s > 0 )
            {
                archiveWrapper.add( getSeparator() );
            }
            calendar.set( Calendar.MONTH, calendar.get( Calendar.MONTH ) - 1 );
            archiveWrapper.add( new Anchor(
                    "",
                    FormatUtil.formatDate( calendar.getTime(), FormatUtil.DATE_FORMAT_MONTH_YEAR ),
                    UriConstructor.createDateUri( Integer.valueOf( calendar.get( Calendar.YEAR ) ).toString(), Integer.valueOf( calendar.get( Calendar.MONTH ) + 1 ).toString(), null )
            ) );
            s++;
        }

        // contact panel
        Panel contactWrapper = new Panel();
        add( contactWrapper );
        contactWrapper.add( new Anchor( "", localize( "contact.label.gmail" ), localize( "contact.href.gmail" ) ) );
        contactWrapper.add( getSeparator() );
        contactWrapper.add( new Anchor( "", localize( "contact.label.linkedin" ), localize( "contact.href.linkedin" ) ) );
        contactWrapper.add( getSeparator() );
        contactWrapper.add( new Anchor( "", localize( "contact.label.facebook" ), localize( "contact.href.facebook" ) ) );

        // copyright
        Span copyright = new Span( localize( "label.copyright", WebApplication.get().getPathByPageClass( About.class ) ) );
        copyright.setClassName( "copyright" );
        add( copyright );
    }

    private Span getSeparator()
    {
        Span separator = new Span( "|" );
        separator.setClassName( "separator" );
        return separator;
    }
}
